package com.attendance.www.hr.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.DepartmentBean;
import com.attendance.www.hr.beans.DepartmentPageBean;
import com.attendance.www.hr.entitys.Department;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IDepartmentService;
import com.attendance.www.hr.service.IRoleService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 部门设置
 * @version 2017/9/27-2017/9/28
 * @author 徐伟
 *
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

	@Resource
	private IDepartmentService departmentService;
	@Resource
	private IRoleService roleService;
	/**
	 * 添加部门信息接口			
	 * @param request
	 * @param DepartmentBean
	 * @return
	 */
	@RequestMapping(value = "/saveDepartment.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveDepartment(HttpServletRequest request, @RequestBody DepartmentBean departmentBean) {
		if (departmentBean != null) {
			if (StringUtils.isBlank(departmentBean.getDepartmentCode()) 
					|| StringUtils.isBlank(departmentBean.getDepartmentName())) {
				throw new ResultFaultException("0_00001");
			}
			if (departmentService.getDepartmentByDepartment(departmentBean)) {
				throw new ResultFaultException("0_00010");
			} else {
				if (departmentService.saveDepartment(departmentBean)) {
					return ResultMap.success();
				} else {
					throw new ResultFaultException("0_00007");
				}
			}
		}
		throw new ResultFaultException("0_00001");
	}
	/**
	 * 根据公司编码集合查询，所属部门
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getDepartmentListByCorporationCodeList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getDepartmentListByCorporationCodeList(HttpServletRequest request,@RequestBody DepartmentBean departmentBean) {
		List<DepartmentBean> departmentBeanList =  departmentService.getDepartmentPageList(departmentBean);
		return ResultMap.success(departmentBeanList);
	}
	/**
	 * 获取部门信息接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getDepartmentList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getDepartmentList(HttpServletRequest request,@RequestBody DepartmentBean departmentBean) {
		Integer pageNo = departmentBean.getPageNo();
		Integer pageCount = departmentBean.getPageCount();

		if (pageNo ==null||pageCount==null) {
			throw new ResultFaultException("0_00001");
		}
		
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		DepartmentPageBean departmentPageBean = new DepartmentPageBean();
		departmentPageBean = departmentService.getDepartmentList(departmentBean, startNum, pageCount);
		return ResultMap.success(departmentPageBean);
	}
	/**
	 * 修改部门信息接口			
	 * @param request
	 * @param DepartmentBean
	 * @return
	 */
	@RequestMapping(value = "/updateDepartment.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateDepartmentList(HttpServletRequest request, @RequestBody DepartmentBean departmentBean) {
		if (departmentBean != null) {
			if (StringUtils.isBlank(departmentBean.getDepartmentCode()) 
					|| StringUtils.isBlank(departmentBean.getDepartmentName())
					|| departmentBean.getDepartmentId()==null) {
				throw new ResultFaultException("0_00001");
			}
			if (departmentService.getDepartmentByDepartment(departmentBean)) {
				throw new ResultFaultException("0_00010");
			}
			Department Department = departmentService.getDepartmentByDepartmentId(departmentBean.getDepartmentId());
			if (Department != null) {
				if (Department.getUpdateTime().compareTo(departmentBean.getUpdateTime()) == 0) {
					if (departmentService.updateDepartment(departmentBean)) {
						return ResultMap.success();
					} else {
						throw new ResultFaultException("0_00007");
					}
				} else {
					throw new ResultFaultException("0_00011");
				}
			} else {
				throw new ResultFaultException("0_00012");
			}
		}
		throw new ResultFaultException("0_00001");
	}
	/**
	 * 删除部门信息接口		
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/deleteDepartment.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteDepartment(HttpServletRequest request) {
		String departmentIdString = request.getParameter("departmentId");
		if (StringUtils.isBlank(departmentIdString)) {
			throw new ResultFaultException("0_00001");
		}
		Integer departmentId = Integer.valueOf(departmentIdString);
		if(departmentService.getEmployeeDepartmentListByDepartmentId(departmentId).size()>0){
			throw new ResultFaultException("0_00016");
		}
		if(roleService.getDepartmentRoidByRoidId(departmentId).size()>0){
			throw new ResultFaultException("0_00026");
		}
		if(departmentService.getDepartmentRoidByPath(departmentId).size()>1){
			throw new ResultFaultException("0_00026");
		}
		return ResultMap.success(departmentService.deleteDepartmentList(departmentId));
	}

}
